<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;

$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return['diy']=$this->plugin_setting('diy');
$basic=$this->plugin_setting('basic');
$return['banner']=$basic['image'];
if($_RQ['op']=='list'){
	$serve_list=pdo_getall('jy_meiye_serve',array('uniacid'=>$_SESSION['uniacid'],'status'=>'1'),'','','paixu asc');
	foreach($serve_list as &$item){
		//已支付，已确认，已完成订单数
		$num1=pdo_count('jy_meiye_order',array('uniacid'=>$_SESSION['uniacid'],'serve_id'=>$item['id'],'status'=>'1'));
		$num2=pdo_count('jy_meiye_order',array('uniacid'=>$_SESSION['uniacid'],'serve_id'=>$item['id'],'status'=>'2'));
		$num3=pdo_count('jy_meiye_order',array('uniacid'=>$_SESSION['uniacid'],'serve_id'=>$item['id'],'status'=>'4'));
		$item['num']=$num1+$num2+$num3;
	}
	$return['post']=array();
	$return['serve_list']=$serve_list;
	exi($return);
}
if($_RQ['op']=='sever_detail'){
	if(!$_RQ['id']){
		exi('没有找到该项目，请重新选择！','error','jy_meiye/sever/list');
	}
	//查询项目具体信息
	$serve=pdo_get('jy_meiye_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	$package=$serve['package'];
	//查询本项目可服务员工(未锁定，状态为可服务)
	$worker_list=pdo_getall('jy_meiye_worker',array('uniacid'=>$_SESSION['uniacid'],'islock'=>'0','status'=>'1'));
	foreach($worker_list as $k=>$item){
		$service=explode(";",$item['service']);
		if(!in_array($_RQ['id'],$service)){
			unset($worker_list[$k]);
		}
	}
	//查询本项目的访问人数及添加访问人数
	$num=pdo_getcolumn('jy_meiye_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']),'num');
	$serve['num']=$num+1;
	pdo_update('jy_meiye_serve',array('num'=>$serve['num']),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
		//已支付，已确认，已完成订单数
	$num1=pdo_count('jy_meiye_order',array('uniacid'=>$_SESSION['uniacid'],'serve_id'=>$_RQ['id'],'status'=>'1'));
	$num2=pdo_count('jy_meiye_order',array('uniacid'=>$_SESSION['uniacid'],'serve_id'=>$_RQ['id'],'status'=>'2'));
	$num3=pdo_count('jy_meiye_order',array('uniacid'=>$_SESSION['uniacid'],'serve_id'=>$_RQ['id'],'status'=>'4'));
	$serve['ordernum']=$num1+$num2+$num3;
	$serve_time=json_decode($serve['serve_time'],true);
	//日期 今天，未来七天
	$data=array();
	$weekarray=array("日","一","二","三","四","五","六"); //先定义一个数组
	$days=$basic['days']>0?$basic['days']:7;
	for($i=0;$i<$days;$i++){
		$data[]=array('riqi'=>date("m-d",strtotime("+".$i." day")),'xinq'=>"星期".$weekarray[date("w",strtotime(date("Y-m-d",strtotime("+".$i." day"))))],'isshow'=>($serve['advancedays']>0&&$i<$serve['advancedays'])?-1:1);
	}
	//获取当前时间
	$time=time();
	$weekid=date("w");
	$weekid=$weekid?:7;
	$iswork=strpos($serve['work_time'],';'.$weekid.';');
	foreach($serve_time as $k=>&$item){
		$num1=pdo_count('jy_meiye_order',array('timeid'=>$k,'uniacid'=>$_SESSION['uniacid'],'day'=>date("Y-m-d",time("day")),'serve_id'=>$_RQ['id'],'status'=>1));
		$num2=pdo_count('jy_meiye_order',array('timeid'=>$k,'uniacid'=>$_SESSION['uniacid'],'day'=>date("Y-m-d",time("day")),'serve_id'=>$_RQ['id'],'status'=>2));
		$num3=pdo_count('jy_meiye_order',array('timeid'=>$k,'uniacid'=>$_SESSION['uniacid'],'day'=>date("Y-m-d",time("day")),'serve_id'=>$_RQ['id'],'status'=>4));
		$num=$num1+$num2+$num3;
		if($item['yuyue']>$num){
		$item['yuyue']=$item['yuyue']-$num;
		}
		else{
		$item['yuyue']=0;
		}
		//时间转为时间戳
		$item['shijian']=strtotime(date("Y-m-d",time("day")).$item['times']['start']);
		if($item['shijian']<time() || $iswork === false){
			$item['show']=0;
		}
		else{
			$item['show']=1;
		}
	}
	$this->session('serve_id',$_RQ['id']);
	$return['serve']=$serve;
	$return['package']=json_decode($package);
	$return['worker_list']=$worker_list;
	$return['serve_time']=$serve_time;
	$return['data']=$data;
	$return['post']=array();
	exi($return);
}
if($_RQ['op']=='notice'){
	$postdata=get_postdata();
	//日期 今天，未来七天
	$data=array();
	$data['0']=date("Y-m-d",time("day")); 
	$data['1']=date("Y-m-d",strtotime("+1 day")); 
	$data['2']=date("Y-m-d",strtotime("+2 day")); 
	$data['3']=date("Y-m-d",strtotime("+3 day")); 
	$data['4']=date("Y-m-d",strtotime("+4 day")); 
	$data['5']=date("Y-m-d",strtotime("+5 day")); 
	$data['6']=date("Y-m-d",strtotime("+6 day"));
	$serve_id = $this->session('serve_id');
	if($postdata){
			//$package=ijson_encode(array('package_title'=>$postdata['package_title'],'package_price'=>$postdata['package_price']));
			//$time=ijson_encode(array('data'=>$postdata['data'],'start'=>$postdata['start'],'end'=>$postdata['end']));
			$package=pdo_getcolumn('jy_meiye_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$serve_id),'package');
			$package=json_decode($package,true);
			$insert=array(
				'uniacid'=>$_SESSION['uniacid'],
				'user_id'=>$_SESSION['uid'],
				'worker_id'=>$postdata['worker_id'],
				'serve_id'=>$this->session('serve_id'),
				'package'=>ijson_encode($package[$postdata['packageid']]),
				'timeid'=>$postdata['timeid'],
				'day'=>$data[$postdata['dayid']],
				'ordersn'=>get_ordersn('jy_meiye_order','ordersn'),
				'createtime'=>TIMESTAMP,
			);
			pdo_insert('jy_meiye_order',$insert);
			$id=pdo_insertid();
	}
	exi('','','jy_meiye/book/main/id='.$id);
}
if($_RQ['op']=='serve_time'){
	
	if($_RQ['id'] == ''){
		exi('请重新选择项目','error');
	}
	if($_RQ['day'] == ''){
		exi('请重新选择日期','error');
	}
	$data=array();
	//$data['0']=date("Y-m-d",time("day")); 
	//$data['1']=date("Y-m-d",strtotime("+1 day")); 
	//$data['2']=date("Y-m-d",strtotime("+2 day")); 
	//$data['3']=date("Y-m-d",strtotime("+3 day")); 
	//$data['4']=date("Y-m-d",strtotime("+4 day")); 
	//$data['5']=date("Y-m-d",strtotime("+5 day")); 
	//$data['6']=date("Y-m-d",strtotime("+6 day"));
	$data=$this->days($_RQ['id'],$basic['days']);
	$return['data']=$data;
	$post=get_postdata();
	$serve_time=pdo_getcolumn('jy_meiye_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']),'serve_time');
	$serve_time=json_decode($serve_time,true);
	//获取当前时间
	$time=time();
	$work_time=pdo_getcolumn('jy_meiye_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']),'work_time');
	$weekid=date("w",strtotime(date("Y-m-d",strtotime("+".$_RQ['day']." day"))));
	$weekid=$weekid?:7;
	$iswork=strpos($work_time,';'.$weekid.';');
	foreach($serve_time as &$item){
		$num=pdo_count('jy_meiye_order',array('timeid'=>$item['id'],'uniacid'=>$_SESSION['uniacid'],'day'=>$data[$_RQ['day']],'serve_id'=>$_RQ['id']));
		if($item['yuyue']>$num){
		$item['yuyue']=$item['yuyue']-$num;
		}
		else{
		$item['yuyue']=0;
		}
		$item['shijian']=strtotime($data[$_RQ['day']]['ymd'].$item['times']['start']);
		if($item['shijian']<time() || $iswork === false){
			$item['show']=0;
		}
		else{
			$item['show']=1;
		}
	}
	$return['serve_time']=$serve_time;
	$return['success']=1;
	
	exi($return);
}
if($_RQ['op']=='package'){
	if(!$_RQ['id']){
		exi('请重新选择项目','error');
	}
	//exi($_RQ['id'],'error');
	$package=pdo_getcolumn('jy_meiye_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']),'package');
	$package=json_decode($package,true);
	$return['package']=$package;
	$return['success']=1;
	exi($return);
}
